package com.yisu.expressway.manager.website.controller.goodsArchives;

import com.google.common.collect.Lists;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.RecommendGoods;
import com.yisu.expressway.manager.core.service.IRecommendGoodsService;
import com.yisu.expressway.manager.domain.request.RecommendGoodsRequest;
import com.yisu.expressway.manager.website.controller.BaseController;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.LongValidator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;

/**
 * Created by xlt on 2017/4/27.
 */
@Controller
@RequestMapping(value = "/recommendGoods")
public class RecommendGoodsController extends BaseController {
    @Resource
    private IRecommendGoodsService iRecommendGoodsService;

    /**
     * 编辑类目推荐商品
     *
     * @param recommendGoodsRequest 推荐商品请求信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyRecommendGoods.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyRecommendGoods(HttpServletRequest request, RecommendGoodsRequest recommendGoodsRequest) {
        try {
            if (StringUtils.isEmpty(recommendGoodsRequest.getCategoryCode()) || StringUtils.isEmpty(recommendGoodsRequest.getRecommendGoodsInfo() + "")) {
                return new JsonResult(FAILURE.code, "参数不完整！");
            }
            List<String> contentInfo = Linq4j.asEnumerable(StringUtils.split(recommendGoodsRequest.getRecommendGoodsInfo(), ",，")).toList();
            List<RecommendGoods> recommendGoodsList = Lists.newArrayList();
            if (contentInfo.size() > 0) {
                for (String item : contentInfo) {
                    String[] eachInfo = StringUtils.split(item,"|");
                    if (eachInfo.length < 2) {
                        continue;
                    }
                    RecommendGoods modelItem = new RecommendGoods();
                    modelItem.setCategoryCode(Long.valueOf(eachInfo[1]));
                    modelItem.setSpecificationNo(Long.valueOf(eachInfo[2]));
                    modelItem.setSourceCode(Long.valueOf(recommendGoodsRequest.getCategoryCode()));
                    modelItem.setGoodsNo(Long.valueOf(eachInfo[0]));
                    recommendGoodsList.add(modelItem);
                }
            }
            if (!CollectionUtils.isEmpty(recommendGoodsList)) {
                if (iRecommendGoodsService.batchAddRecommendGoods(recommendGoodsList)) {
                    return new JsonResult();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }

    /**
     * 删除类目推荐商品
     *
     * @param recommendGoods 推荐商品请求信息
     * @return 状态
     */
    @RequestMapping(value = "/deleteRecommendGoods.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteRecommendGoods(HttpServletRequest request, RecommendGoods recommendGoods) {
        try {
            if (recommendGoods.getId()==null) {
                return new JsonResult(FAILURE.code, "参数不完整！");
            }
            if (iRecommendGoodsService.deleteRecommendGoodsByPrimaryKey(recommendGoods.getId())) {
                return new JsonResult();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }

    /**
     * 编辑类目推荐商品
     *
     * @param recommendGoods 推荐商品请求信息
     * @return 状态
     */
    @RequestMapping(value = "/updateRecommendGoodsSort.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult updateRecommendGoodsSort(HttpServletRequest request, RecommendGoods recommendGoods) {
        try {
            if (recommendGoods.getCategoryCode() == null || recommendGoods.getGoodsNo() == null || recommendGoods.getSort() == null) {
                return new JsonResult(FAILURE.code, "参数不完整！");
            }
            if (iRecommendGoodsService.updateSort(recommendGoods)) {
                return new JsonResult();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }

}
